package com.zzyl.nursing.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class WechatServiceImpl implements WechatService {
    //微信请求地址
    private final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code";

    // 获取token
    private final String CHECKSESSIONKEY_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    //获取手机号的临时凭证
    private final String GETPHONENUMBER_URL="https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";


    @Value("${wechat.appid}")
    private String appid ;
    @Value("${wechat.appsecret}")
    private String secret ;
    @Override
    public String getOpenid(String code) {
        // 1-请求
        Map<String, Object> paramMap = MapUtil.<String, Object>builder()
                .put("appid", appid)
                .put("secret", secret)
                .put("js_code", code)
                .put("grant_type", "authorization_code")
                .build();
        //发送请求
        String result = HttpUtil.get(REQUEST_URL, paramMap);
        //2-解析结果:判断如果是成功才能获取对应的值
        JSONObject jsonObject = JSONUtil.parseObj(result);
        Integer errcode = jsonObject.getInt("errcode");
        //如果errcode 不为空，则说明请求失败
        if (ObjectUtil.isNotNull(errcode)){
            throw new HttpException(jsonObject.getStr("errmsg"));
        }
        return jsonObject.getStr("openid");
    }

    @Override
    public String getAccessToken() {
        Map<String, Object> paramMap = MapUtil.<String, Object>builder()
                .put("appid", appid)
                .put("secret", secret)
                .put("grant_type", "client_credential")
                .build();
        String resultJson = HttpUtil.get(CHECKSESSIONKEY_URL, paramMap);
        JSONObject jsonObject = JSONUtil.parseObj(resultJson);
        Integer errcode = jsonObject.getInt("errcode");
        if (ObjectUtil.isNotNull(errcode)){
            throw new HttpException(jsonObject.getStr("errmsg"));
        }
        return jsonObject.getStr("access_token");
    }

    @Override
    public String getPhone(String phoneCode) {
        Map<String, Object> bodyMap = MapUtil.<String, Object>builder()
                .put("code", phoneCode)
                .build();
        String resultJson = HttpUtil.post(GETPHONENUMBER_URL + getAccessToken(), JSONUtil.toJsonStr(bodyMap));
        JSONObject jsonObject = JSONUtil.parseObj(resultJson);
        Integer errcode = jsonObject.getInt("errcode");
        if (errcode!=0){
            throw new HttpException(jsonObject.getStr("errmsg"));
        }
        JSONObject phoneInfo = jsonObject.getJSONObject("phone_info");
        return phoneInfo.getStr("phoneNumber");
    }
}
